---
--类名: MakeItem.lua
--创建日期: 2017-12-22
--创建人: 陈小虎
--功能描述: 
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--

local MakeItem = class("MakeItem")

function MakeItem:ctor()

end

function MakeItem:createWithData(node,data)
	local itemNode = nil
    if data.nodeMenuLevel == 1 then
        itemNode = cc.CSLoader:createNode(Res.MakeTypeNode)
    elseif data.nodeMenuLevel == 2 then
        itemNode = cc.CSLoader:createNode(Res.MakeSubTypeNode)
    else
        itemNode = cc.CSLoader:createNode(Res.MakeNode)
    end


	self:init(data,itemNode)
	return itemNode
end

function MakeItem:init(data,itemNode)
	local root = itemNode:getChildByName("Panel")
	itemNode:setContentSize(root:getContentSize())
	itemNode.level = data.nodeMenuLevel

	self._root = root
	self._menuLevel = data.nodeMenuLevel
	self._selectedBtn = root:getChildByName("selected_btn")
	self._img1 = root:getChildByName("img_1")
	self._img2 = root:getChildByName("img_2")
	self._name = root:getChildByName("name")
	self._lv = root:getChildByName("lv") -- 已经在资源里隐藏了
	self._dot = root:getChildByName("dot")

	self._nameStr = ""
	self._lvStr = ""
	
	-- root:setBackGroundColorType(1)
	-- root:setBackGroundColor(cc.c3b(255,255,255))

	if self._menuLevel == 1 then
		self._root:setOpacity(0)
	end

	if self._selectedBtn then
		self._selectedBtn:setEnabled(true)
		self._selectedBtn:setTouchEnabled(true)
		self._selectedBtn:setBright(true)
		self._selectedBtn:setZoomScale(0)
		self._selectedBtn:addClickEventListener(function(sender)
			self:onClick()
		end)
	end


	if self._menuLevel == 3 then
		local bg1 = root:getChildByName("btn_bg_1")
		local bg2 = root:getChildByName("btn_bg_2")
		local bg_select = root:getChildByName("btn_bg_select")
		bg1:setVisible(true)
		bg2:setVisible(false)
		bg_select:setVisible(false)


		bg1:setTouchEnabled(true)
		bg2:setTouchEnabled(true)
		bg_select:setTouchEnabled(true)
		bg1:addClickEventListener(function(sender)
			self:onClick()
		end)
		bg2:addClickEventListener(function(sender)
			self:onClick()
		end)
		bg_select:addClickEventListener(function(sender)
			self:onClick()
		end)
	end

	self:setId(data.id)
	self:setName(data.name)
	self:setLevel(data.level)
	self:updateQualityColor(data)
	self:updateMenuStringColor(false)
	self:setBg1Visible(false)
	--若該配方是新配方 則需要現在紅點
	self:setDotVisible(UD:isFormulaNewGet(data.id))
end

function MakeItem:updateLastItem(isLast)
	if self._menuLevel == 1 then return end
	if isLast then
		local borderH = (self._menuLevel == 2) and 3 or 4
		local bottomBorder = ccui.Layout:create()
		self._bottomBorder = bottomBorder
		bottomBorder:setContentSize(cc.size(self._root:getContentSize().width,borderH))
		bottomBorder:setPosition(cc.p(0,-borderH))
		self._root:addChild(bottomBorder,-1)
		bottomBorder:setBackGroundColorType(1)
		bottomBorder:setBackGroundColor(cc.c3b(99,99,99))
	elseif self._bottomBorder then
		self._root:removeChild(self._bottomBorder)
		self._bottomBorder = nil
	end
	
end

function MakeItem:setOnClick(click)
	self._click = click
end

function MakeItem:onClick()
	if self._click then
		self._click()
	end
end

function MakeItem:setId(id)
	self._id = id
end

function MakeItem:getId()
	return self._id
end

function MakeItem:setName(name)
	if not name then return end
	self._nameStr = name
	self._name:setString(name)
	self:setTotalDesc()
end

function MakeItem:updateQualityColor(data)
	if data.nodeMenuLevel == 3 then
        local color = Enums.QualityColor[tonumber(data.quality)]
        if color then
            self._name:setTextColor(color)
        end

		local lock = self._root:getChildByName("lock")
		local isOpen = UD:isHasFormula(data.id,data.skillId)
		lock:setVisible(not isOpen)
		local lockBg = self._root:getChildByName("btn_bg_2")
		lockBg:setVisible(not isOpen)

	end
end

function MakeItem:updateToClassName()
	local name = L("manufacturingname_"..tostring(self._id))
	
	-- if UD.player.classID == Enums.Profession.warrior then
	-- 	name = L("manufacturingname_101")
	-- elseif (UD.player.classID == Enums.Profession.mage) or (UD.player.classID == Enums.Profession.preist) then
	-- 	name = L("manufacturingname_102")
	-- elseif UD.player.classID == Enums.Profession.rouge then
	-- 	name = L("manufacturingname_103")
	-- end
	self:setName(name)
end

function MakeItem:getName()
	-- return self._name:getString()
	return self._nameStr
end

function MakeItem:setTotalDesc()
	if self._lv then
		self._name:setString(self._nameStr .. " " .. self._lvStr)
	end
end

function MakeItem:setDotVisible(bl)
	self._dot:setVisible(bl)
end

function MakeItem:updateMenuStringColor(isOpen)
	if self._menuLevel == 1 then
		if isOpen then
			self._lv:setTextColor(cc.c4b(255, 225, 132, 255))
			self._name:setTextColor(cc.c4b(255, 225, 132, 255))
		else
			self._lv:setTextColor(cc.c4b(109, 109, 109, 255))
			self._name:setTextColor(cc.c4b(109, 109, 109, 255))
		end
	elseif self._menuLevel == 2 then
		if isOpen then
			self._name:setTextColor(cc.c4b(255, 225, 132, 255))
		else
			self._name:setTextColor(cc.c4b(109, 109, 109, 255))
		end
	end
end

function MakeItem:setLevel(level)
	if self._lv then
		local levelStr = L("text_10000098") .. " " .. level
		self._lvStr = levelStr
		self._lv:setString(levelStr)
		self:setTotalDesc()
	end
end

function MakeItem:switchOn(isOpen)
	if self._selectedBtn then
		self._selectedBtn:setBright(not isOpen)
	end

	self:updateMenuStringColor(isOpen)
	if self._menuLevel == 1 then
		self._root:setOpacity(isOpen and 255 or 0)
	elseif self._menuLevel == 3 then
		local normalBg = self._root:getChildByName("btn_bg_1")
		normalBg:setVisible(not isOpen)

		local selectBg = self._root:getChildByName("btn_bg_select")
		selectBg:setVisible(isOpen)
	end
	self:setBg1Visible(isOpen)
end

function MakeItem:showOpenArrow(isOpen) 
	if not self._img1 or not self._img2 then return end
	self._img1:setVisible(isOpen)
	self._img2:setVisible(not isOpen)
end


function MakeItem:setBg1Visible(trueOrFalse)
	if self._menuLevel == 2 then
		local bg1 = self._root:getChildByName("bg_1")
		if bg1 then
			bg1:setVisible(trueOrFalse)
		end
	end
end
return MakeItem